<#
#  It is recommended to test the script on a local machine for its purpose and effects. 
#  ManageEngine Endpoint Central will not be responsible for any 
#  damage/loss to the data/setup based on the behavior of the script.

#  Description: Script to delete local user profile not used for X days except users hardcoded inside the script
#  Script Arguments: "No. of days not used"
#  Configuration Type - COMPUTER
#  Note: User Names need to be hardcoded inside the script
#>

if ($args[0] -eq $null) {
    Write-Host "Please enter number of days in Script Arguments"
    exit 1
}

$dys = $args[0]
$excludedUsers = @("Administrator", "Default", "Public", "ekvdbeheer", "ekvdbeheer.EKVD")  #The values need to be hardcoded here
$ProfileInfo = Get-WmiObject -Class Win32_UserProfile | Where-Object {
    $_.ConvertToDateTime($_.LastUseTime) -le (Get-Date).AddDays(-$dys) -and 
    $_.LocalPath -notlike "*$env:SystemRoot*" -and 
    $excludedUsers -notcontains $_.LocalPath.Split("\")[-1]
}

if ($ProfileInfo -eq $null) {
    Write-Host "No profiles to be deleted"
} elseif ($dys -gt 0) {
    foreach ($usr in $ProfileInfo) {
        Write-Host $usr.LocalPath
        Try {
            $usr.Delete()
            Write-Host "Deleted profile successfully."
        } Catch {
            Write-Host "Delete profile failed."
        }
    }
}